package br.com.petrobras.iam.plugins.register.local;

import oracle.iam.platform.OIMClient;
import oracle.iam.platform.pluginframework.PluginException;
import oracle.iam.platformservice.api.PlatformService;
import oracle.iam.platformservice.exception.PlatformServiceAccessDeniedException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import br.com.petrobras.iam.plugins.register.factory.PegarClienteOIM ;


public class RegisterLocal {

    private static OIMClient oimClient;


    public RegisterLocal() {
		super();
		// TODO Auto-generated constructor stub
	}
    public RegisterLocal(OIMClient param) {
		super();
        oimClient = param;
	}

    

	public static void main(String args[]) {
        try {
            RegisterLocal oimSample = new RegisterLocal();
            oimSample.init();
            oimSample.PluginRegister();
            oimClient.logout();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
      * Initialize the context and login with client supplied environment
      */
    public void init() throws Exception
    {
        oimClient = new PegarClienteOIM().conexao();
    }

    public void PluginRegister() throws FileNotFoundException, IOException,
            PlatformServiceAccessDeniedException, PluginException {
        System.out.println("(Un)Registering plugin...");

        //String arquivo_zip = "D:\\programas\\OIM_ZIP\\plugin.zip";
        String arquivo_zip = "C:\\Users\\wtno\\workspace\\plugins\\target\\plugins.zip";

        PlatformService service = oimClient.getService(PlatformService.class);
        File zipFile = new File(arquivo_zip);
        FileInputStream fis = new FileInputStream(zipFile);
        int size = (int) zipFile.length();
        byte[] b = new byte[size];
        int bytesRead = fis.read(b, 0, size);
        while (bytesRead < size) {
            bytesRead += fis.read(b, bytesRead, size - bytesRead);
        }
        fis.close();
        //service.unRegisterPlugin("br.com.petrobras.iam.tarefas.SampleScheduledTask", "1.0");
        service.registerPlugin(b);
        System.out.println("Plugin (Un)Registered successfuly");
//		<plugin pluginclass= "br.com.petrobras.iam.tarefas.SampleScheduledTask" version="1.0" name="SampleScheduledTask">
    }


}

// qior_oim       | oimdsv_02
// qiam           | oimd_qiam0212
// qiam_etl       | etlqiam_oimd44
// qiam_aplicacao | oimdqiamapp_0231

